{% extends "base.html" %}

{% block title %}分析结果 - {{ super() }}{% endblock %}

{% block extra_css %}
<link href="/static/css/pages/ai-analysis/analysis-results.css" rel="stylesheet">
<link href="/static/css/analysis-result-details.css" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="space-y-6">
    <!-- 页面头部 -->
    <div class="flex justify-between items-center">
        <div>
            <h2 class="text-xl font-semibold text-slate-800">分析结果查询</h2>
            <p class="text-slate-600 mt-1">查看不同类型AI分析任务的执行结果</p>
        </div>
        <div class="flex space-x-3">
            <button id="exportResultsBtn" 
                    class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors">
                <i class="fas fa-download mr-2"></i>导出结果
            </button>
            <button id="refreshResultsBtn" 
                    class="px-4 py-2 border border-slate-300 text-slate-700 rounded-lg hover:bg-slate-50 transition-colors">
                <i class="fas fa-sync-alt mr-2"></i>刷新
            </button>
        </div>
    </div>

    <!-- 分析类型标签页 -->
    <div class="bg-white rounded-xl shadow-sm border border-slate-200">
        <div class="flex space-x-1 p-1">
            <button class="analysis-tab active flex-1 py-3 px-4 rounded-lg font-medium" 
                    data-tab="regional">
                <i class="fas fa-map-marker-alt mr-2"></i>区域分析
            </button>
            <button class="analysis-tab flex-1 py-3 px-4 rounded-lg font-medium" 
                    data-tab="complaint-type">
                <i class="fas fa-tags mr-2"></i>投诉类型
            </button>
            <button class="analysis-tab flex-1 py-3 px-4 rounded-lg font-medium" 
                    data-tab="sentiment">
                <i class="fas fa-heart mr-2"></i>情感分析
            </button>
            <button class="analysis-tab flex-1 py-3 px-4 rounded-lg font-medium" 
                    data-tab="compliance">
                <i class="fas fa-check-circle mr-2"></i>规范性分析
            </button>
            <button class="analysis-tab flex-1 py-3 px-4 rounded-lg font-medium" 
                    data-tab="duplicate">
                <i class="fas fa-copy mr-2"></i>重复分析
            </button>
        </div>
    </div>

    <!-- 查询条件区域 -->
    <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-4">
        <div class="grid grid-cols-1 md:grid-cols-5 gap-4">
            <!-- 任务选择 -->
            <div class="filter-select-wrapper">
                <select id="taskFilter"
                        class="w-full px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <option value="">全部任务</option>
                </select>
            </div>

            <!-- 区域选择 (动态加载) -->
            <div id="regionFilterContainer">
                <div class="flex space-x-2">
                    <div class="filter-select-wrapper flex-1">
                        <select id="districtFilter"
                                class="w-full px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                            <option value="">全部区县</option>
                        </select>
                    </div>
                    <div class="filter-select-wrapper flex-1">
                        <select id="streetFilter"
                                class="w-full px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                            <option value="">全部街道</option>
                        </select>
                    </div>
                </div>
            </div>

            <!-- 类型筛选 (根据当前标签页动态变化) -->
            <div class="filter-select-wrapper">
                <select id="typeFilter"
                        class="w-full px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <option value="">全部类型</option>
                </select>
            </div>

            <!-- 时间范围 -->
            <div>
                <div class="flex space-x-2">
                    <input type="date" id="startDateFilter" 
                           class="flex-1 px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                    <input type="date" id="endDateFilter" 
                           class="flex-1 px-3 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>

            <!-- 搜索和操作 -->
            <div class="flex space-x-2">
                <div class="flex-1 relative">
                    <i class="fas fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-slate-400"></i>
                    <input type="text" id="searchInput" placeholder="搜索..." 
                           class="w-full pl-10 pr-4 py-2 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500">
                </div>
                <button id="resetFiltersBtn" 
                        class="px-4 py-2 border border-slate-300 rounded-lg hover:bg-slate-50 transition-colors">
                    <i class="fas fa-redo"></i>
                </button>
            </div>
        </div>
    </div>

    <!-- 结果统计卡片 -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
        <div class="stat-card bg-white rounded-xl border border-slate-200 p-6 hover:shadow-lg transition-all duration-300">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-slate-600 font-medium">总结果数</p>
                    <p class="text-3xl font-bold text-slate-800 mt-1" id="totalResults">0</p>
                </div>
                <div class="w-14 h-14 bg-gradient-to-br from-blue-400 to-blue-600 rounded-xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-database text-white text-xl"></i>
                </div>
            </div>
            <div class="mt-4 h-1 bg-gradient-to-r from-blue-100 to-transparent rounded-full"></div>
        </div>
        <div class="stat-card bg-white rounded-xl border border-slate-200 p-6 hover:shadow-lg transition-all duration-300">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-slate-600 font-medium">今日新增</p>
                    <p class="text-3xl font-bold text-slate-800 mt-1" id="todayResults">0</p>
                </div>
                <div class="w-14 h-14 bg-gradient-to-br from-green-400 to-green-600 rounded-xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-plus text-white text-xl"></i>
                </div>
            </div>
            <div class="mt-4 h-1 bg-gradient-to-r from-green-100 to-transparent rounded-full"></div>
        </div>
        <div class="stat-card bg-white rounded-xl border border-slate-200 p-6 hover:shadow-lg transition-all duration-300">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-slate-600 font-medium">平均置信度</p>
                    <p class="text-3xl font-bold text-slate-800 mt-1" id="avgConfidence">0%</p>
                </div>
                <div class="w-14 h-14 bg-gradient-to-br from-yellow-400 to-yellow-600 rounded-xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-percentage text-white text-xl"></i>
                </div>
            </div>
            <div class="mt-4 h-1 bg-gradient-to-r from-yellow-100 to-transparent rounded-full"></div>
        </div>
        <div class="stat-card bg-white rounded-xl border border-slate-200 p-6 hover:shadow-lg transition-all duration-300">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-sm text-slate-600 font-medium">覆盖率</p>
                    <p class="text-3xl font-bold text-slate-800 mt-1" id="coverageRate">0%</p>
                </div>
                <div class="w-14 h-14 bg-gradient-to-br from-purple-400 to-purple-600 rounded-xl flex items-center justify-center shadow-lg">
                    <i class="fas fa-chart-pie text-white text-xl"></i>
                </div>
            </div>
            <div class="mt-4 h-1 bg-gradient-to-r from-purple-100 to-transparent rounded-full"></div>
        </div>
    </div>

    <!-- 结果表格 -->
    <div class="bg-white rounded-xl shadow-sm border border-slate-200 overflow-hidden">
        <!-- 表格内容将通过JavaScript动态生成 -->
        <div id="resultsTableContainer">
            <!-- 加载状态 -->
            <div id="resultsLoadingState" class="hidden text-center py-12">
                <div class="inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-indigo-600"></div>
                <p class="mt-2 text-slate-600">正在加载数据...</p>
            </div>

            <!-- 空状态 -->
            <div id="resultsEmptyState" class="hidden text-center py-12">
                <i class="fas fa-inbox text-4xl text-slate-300 mb-4"></i>
                <p class="text-slate-500">暂无数据</p>
            </div>

            <!-- 表格区域 -->
            <div id="resultsTableArea" class="hidden">
                <div class="overflow-x-auto">
                    <table class="w-full divide-y divide-slate-200 table-auto">
                        <thead class="bg-slate-50">
                            <tr id="resultsTableHeader">
                                <!-- 表头将通过JavaScript动态生成 -->
                            </tr>
                        </thead>
                        <tbody id="resultsTableBody" class="bg-white divide-y divide-slate-200">
                            <!-- 数据行将通过JavaScript动态生成 -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 分页 -->
    <div id="resultsPagination" class="hidden bg-white rounded-xl shadow-sm border border-slate-200 p-4">
        <div class="flex flex-col lg:flex-row items-center justify-between gap-4">
            <!-- 左侧：页面显示信息 -->
            <div class="flex items-center gap-4">
                <span class="text-sm text-slate-700">
                    显示第 <span id="pageStart" class="font-medium">0</span> 到 <span id="pageEnd" class="font-medium">0</span> 条，
                    共 <span id="totalItems" class="font-medium">0</span> 条记录
                </span>

                <!-- 页面大小选择器 -->
                <div class="flex items-center gap-2">
                    <label for="pageSizeSelect" class="text-sm text-slate-600">每页显示：</label>
                    <div class="filter-select-wrapper" style="width: auto;">
                        <select id="pageSizeSelect"
                                class="px-3 py-1 border border-slate-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 text-sm"
                                style="min-width: 80px;">
                            <option value="5">5条</option>
                            <option value="10">10条</option>
                            <option value="15">15条</option>
                            <option value="20">20条</option>
                            <option value="30">30条</option>
                            <option value="50">50条</option>
                            <option value="100">100条</option>
                        </select>
                    </div>
                </div>
            </div>

            <!-- 右侧：分页按钮 -->
            <div class="pagination flex items-center gap-1" id="paginationContainer">
                <!-- 分页按钮将通过JavaScript动态生成 -->
            </div>
        </div>
    </div>
</div>

<!-- 详情模态框 -->
<div id="resultDetailModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
    <div class="bg-white rounded-xl shadow-2xl max-w-4xl w-full mx-4 max-h-[90vh] overflow-y-auto">
        <div class="p-6 border-b border-slate-200">
            <div class="flex items-center justify-between">
                <h3 class="text-xl font-semibold text-slate-800">分析结果详情</h3>
                <button id="closeDetailModalBtn1" class="text-slate-400 hover:text-slate-600">
                    <i class="fas fa-times text-xl"></i>
                </button>
            </div>
        </div>
        <div id="resultDetailContent" class="p-6">
            <!-- 详情内容将通过JavaScript动态生成 -->
        </div>
        <div class="p-6 border-t border-slate-200 flex justify-end">
            <button id="closeDetailModalBtn2" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors">
                关闭
            </button>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="/static/js/analysis-results.js?v={{ timestamp }}"></script>
<!-- 测试脚本 - 仅在开发环境加载 -->
{% if config.DEBUG %}
<script src="/static/js/test/analysis-results-test-suite.js"></script>
{% endif %}
{% endblock %}